package NWPackage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import Writers.MsgLog;

import orbital.math.MathUtilities;


public class Calculations {
	public static int numCandidates;
	public static int numAgents;
	
	//we are using this methode
	public static int calcCandidatesPermutationsNPR(int numCandidates)
	{
		return MathUtilities.nPr(numCandidates, numCandidates);
	}
	
	private static int calcPairCandidatesPermutationsNCR(int numCandidates)
	{
		return MathUtilities.nCr(numCandidates, 2);
	}
	
	public static int calcNumberOfQueries()
	{
		return calcPairCandidatesPermutationsNCR(numCandidates)*numAgents;
	}
	
	public static double Log2(double number)
	{
		double ans =  (Math.log(number)/Math.log(2.0));
		if (ans == Double.NaN || ans == Double.NEGATIVE_INFINITY || ans == Double.POSITIVE_INFINITY)
			ans = 0;
		return ans;
	}
	public static double Sum(double[] array)
	{
		double sum = 0.0;
		for (double d : array) {
			sum+=d;
		}
		return sum;
	}
	public static double Avg(double[] array)
	{
		int i;
		double avg = 0.0;
		for (i = 0; i < array.length; i++) {
			avg+=array[i];
		}
		return avg/i;
	}

	public static List<List<String>> GetPermutations(List<String> dynamicCandidates)throws Exception {
		List<List<String>> allPermutations = new ArrayList<List<String>>(calcCandidatesPermutationsNPR(numCandidates));
		String candidates = StringFromList(dynamicCandidates);
		
		//candidates = candidates.replace(",", "");
		permuteString1(allPermutations,"",candidates);
		MsgLog.write_to_init("Done calculate all possible permutations of candidates orders");
		for (int i=0; i<allPermutations.size();i++){
			MsgLog.write_to_init(allPermutations.get(i).toString());
		}
		return allPermutations;
	}

	//use this methode when candidates' names are over 1 char (example - c1,c2 etc... and not 1,2)
	private static void permuteString1(List<List<String>> allPermutations,String beginningString, String endingString) throws Exception{
		String[] endArray = endingString.split(",");		
		if (endArray.length <= 1)
	    	allPermutations.add(StringToList(beginningString + endingString));
	    else{
	    	for (int i = 0; i < endArray.length; i++) {
	        try {
	          String newString = SubArray(endArray,0, i) + SubArray(endArray,i + 1,endArray.length);
	          if (newString.length() > 1)
	        	  newString = newString.substring(0, newString.length()-1);
	          permuteString1(allPermutations,beginningString + endArray[i]+",", newString);
	        } 
	        catch (Exception exception) {
	          exception.printStackTrace();
	        }
	      }
	    }
	}
	public static String SubArray(String[] endArray, int start_index, int end_index) {
		String ans = "";
		for (int i = start_index; i < end_index; i++) {
			ans += endArray[i]+",";
		}
		return ans;
	}

	public static List<String> StringToList(String string)throws Exception {
		List<String> list = new ArrayList<String>();
		try{
			if (string!= null && !string.isEmpty())
			{
				String[] temp = string.split(",");
				list = Arrays.asList(temp);
			}
		}
		catch(Exception ex)
		{
			MsgLog.write_to_init("something wrong on StringToList Function");
		}
		//for (int i = 0; i < string.length(); i++) {
		//	list.add(i, String.valueOf(string.charAt(i)));
		//}
		return list;
	}

	public static String StringFromList(List<String> dynamicCandidates) {
		String ans="";
		for (String string : dynamicCandidates) {
			ans+=string + ",";
		}
		ans = ans.substring(0, ans.length()-1);
		return ans;
	}

	
	
	//use this methode when candidates' names are contain only 1 char (example: 1,2...)
	/*private static void permuteString(List<List<String>> allPermutations,String beginningString, String endingString) {
		if (endingString.length() <= 1)
			allPermutations.add(StringToList(beginningString + endingString));
	    else{
	    	for (int i = 0; i < endingString.length(); i++) {
	    		try {
	    			String newString = endingString.substring(0, i) + endingString.substring(i + 1);

	    			permuteString(allPermutations,beginningString + endingString.charAt(i), newString);
    			}
	    		catch (StringIndexOutOfBoundsException exception) {
	    			exception.printStackTrace();
	    		}
	    	}
		}
	}*/
}
